class_name LogicalEffect_OpenArea2DDetect
extends LogicalEffectBase

#region 属性
@export var detector_id:int
@export var is_clear:bool
@export var is_reset_mask:bool
@export var is_active_area:bool
@export var is_add_callback:bool
@export_flags("TargetCamp","SameCamp") var target_camp:int
@export_flags_2d_physics var extra_mask:int
@export var effects:Array[Entity_AdditionalEffect]
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
#endregion
#region 公共方法
func init_effect(manager:LogicalEffectComponent):
	super.init_effect(manager)
## 应用
func apply():
	super.apply()
	var component:Area2DDetectComponent = manager.cur_entity.get_component_by_name("Area2DDetectComponent")
	if component:
		component = component as Area2DDetectComponent
		var detector:Area2DDetector = component.get_detector(detector_id)
		if detector:
			if is_clear:
				detector.clear_detect_target_dict()
			if is_reset_mask:
				var mask:int = _get_final_mask()
				detector.set_layer_mask(mask)
			if is_active_area:
				detector.set_area2d_active(true)
			if is_add_callback and not detector.on_detect_target.is_connected(on_detect_target_func):
				detector.set_on_detect_target(on_detect_target_func,true)
			detector.set_active(true)
## 移除
func remove():
	super.remove()
	var component:Area2DDetectComponent = manager.cur_entity.get_component_by_name("Area2DDetectComponent")
	if component:
		component = component as Area2DDetectComponent
		var detector:Area2DDetector = component.get_detector(detector_id)
		if detector:
			if is_clear:
				detector.clear_detect_target_dict()
			if is_active_area:
				detector.set_area2d_active(false)
			if is_add_callback and detector.on_detect_target.is_connected(on_detect_target_func):
				detector.set_on_detect_target(on_detect_target_func,false)
			detector.set_active(false)
## 更新
func update():
	super.update()
## 刷新
func refresh():
	super.refresh()
## 完成
func complete():
	super.complete()
	var component:Area2DDetectComponent = manager.cur_entity.get_component_by_name("Area2DDetectComponent")
	if component:
		component = component as Area2DDetectComponent
		var detector:Area2DDetector = component.get_detector(detector_id)
		if detector:
			if is_clear:
				detector.clear_detect_target_dict()
			if is_active_area:
				detector.set_area2d_active(false)
			if is_add_callback and detector.on_detect_target.is_connected(on_detect_target_func):
				detector.set_on_detect_target(on_detect_target_func,false)
			detector.set_active(false)
func can_trigger() -> bool:
	return super.can_trigger()
func get_life_state() -> bool:
	return super.get_life_state()
	
func on_detect_target_func(target):
	for effect in effects:
		effect.add_effect(manager.cur_entity,target)
#endregion
#region 私有方法
func _get_final_mask():
	var mask:int = extra_mask
	var camp_component = manager.cur_entity.get_component_by_name("CampComponent") as CampComponent
	var targets:Array[GameEnum.Camp] = []
	if (1 << 0) & target_camp > 0:
		targets.append_array(camp_component.get_target_camps())
	if (1 << 1) & target_camp > 0:
		targets.append_array(camp_component.get_camp_camps())
	for target_camp in targets:
		var layer_mask = manager.cur_entity.get_entity_data().get_layer_mask_by_tag(GameEnum.Camp.keys()[target_camp])
		mask |= layer_mask
	return mask
#endregion
#region 生命周期函数
#endregion
